//
//  CYLogInViewController.m
//  cyjf
//
//  Created by tauCross on 2017/5/3.
//  Copyright © 2017年 Auto Financing. All rights reserved.
//

#import "CYLogInViewController.h"

#import "CYDataAPIRegisterModel.h"

#import "CYSignUpViewController.h"
#import "CYFindPasswordViewController.h"
#import "CYSudokuSignInOpenViewController.h"
#import "CYNavigationController.h"

#import "CYAuthManager.h"
#import "CYPublicParameters.h"
#import "CYGlobal.h"



@interface CYLogInViewController ()

@property(nonatomic, strong)UIImageView *viImageView;
@property(nonatomic, strong)UIView *inputBox;
@property(nonatomic, strong)UITextField *phoneTextField;
@property(nonatomic, strong)UIView *phoneLine;
@property(nonatomic, strong)UITextField *passwordTextField;
@property(nonatomic, strong)UIView *passwordLine;
@property(nonatomic, strong)UIButton *findPasswordButton;
@property(nonatomic, strong)UIButton *logInButton;
@property(nonatomic, strong)UIButton *signUpButton;

@end


@implementation CYLogInViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    CYSharedGlobal.loginShowing = YES;
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    CYSharedGlobal.loginShowing = NO;
}

- (void)loadUI
{
    self.navigationItem.title = @"登录";
    UIImage *image = [CYImage(@"public_nav_close") imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStylePlain target:self action:@selector(close)];
    self.navigationItem.leftBarButtonItem = item;
    
    self.tableView.hidden = YES;
    self.contentView.backgroundColor = HEXCOLOR(@"FFFFFF");
    
    self.viImageView = [[UIImageView alloc] initWithImage:CYImage(@"log_in_vi")];
    [self.contentView addSubview:self.viImageView];
    
    self.inputBox = [[UIView alloc] init];
    [self.contentView addSubview:self.inputBox];
    {
        self.phoneLine = [UIView cy_logInLine];
        [self.inputBox addSubview:self.phoneLine];
        
        self.passwordLine = [UIView cy_logInLine];
        [self.inputBox addSubview:self.passwordLine];
        
        self.phoneTextField = [UITextField cy_logInTextFieldWithPlaceholder:@"手机号"];
        self.phoneTextField.keyboardType = UIKeyboardTypeNumberPad;
        [self.inputBox addSubview:self.phoneTextField];
        
        self.passwordTextField = [UITextField cy_logInTextFieldWithPlaceholder:@"密码"];
        self.passwordTextField.secureTextEntry = YES;
        [self.passwordTextField addTarget:self action:@selector(onPasswordTextFiedldEditingDidEndOnExit) forControlEvents:UIControlEventEditingDidEndOnExit];
        [self.inputBox addSubview:self.passwordTextField];
    }
    
    self.findPasswordButton = [UIButton buttonWithType:UIButtonTypeSystem];
    self.findPasswordButton.tintColor = APP_DEFAULT_COLOR_BLUE;
    [self.findPasswordButton setTitle:@"忘记密码" forState:UIControlStateNormal];
    self.findPasswordButton.titleLabel.font = [UIFont systemFontOfSize:14 weight:UIFontWeightRegular];
    [self.findPasswordButton addTarget:self action:@selector(onFindPasswordButtonTouchUpInside) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:self.findPasswordButton];
    
    self.logInButton = [UIButton cy_mainButtonWithTitle:@"登录"];
    [self.logInButton addTarget:self action:@selector(onLogInButtonTouchUpInside) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:self.logInButton];
    
    self.signUpButton = [UIButton cy_hintButtonWithDesc:@"没有账号？" content:@"去注册" fontSize:14];
    [self.signUpButton addTarget:self action:@selector(onSignUpButtonTouchUpInside) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:self.signUpButton];
}

- (void)layoutUI
{
    self.viImageView.centerX = self.contentView.width / 2;
    self.viImageView.centerY = self.contentView.height * 87 / 602;
    
    self.inputBox.height = 116;
    self.inputBox.width = self.contentView.width - 80;
    self.inputBox.centerX = self.contentView.width / 2;
    self.inputBox.centerY = self.contentView.height * 247 / 602;
    {
        self.phoneLine.width = self.inputBox.width;
        self.phoneLine.height = 1 / SCREEN_SCALE;
        self.phoneLine.left = 0;
        self.phoneLine.bottom = self.inputBox.height / 2;
        
        self.passwordLine.width = self.inputBox.width;
        self.passwordLine.height = 1 / SCREEN_SCALE;
        self.passwordLine.left = 0;
        self.passwordLine.bottom = self.inputBox.height;
        
        self.phoneTextField.width = self.inputBox.width;
        self.phoneTextField.height = 35;
        self.phoneTextField.left = 0;
        self.phoneTextField.bottom = self.phoneLine.bottom;
        
        self.passwordTextField.width = self.inputBox.width;
        self.passwordTextField.height = 35;
        self.passwordTextField.left = 0;
        self.passwordTextField.bottom = self.passwordLine.bottom;
    }
    
    self.findPasswordButton.width = 60;
    self.findPasswordButton.height = 44;
    self.findPasswordButton.right = self.inputBox.right;
    self.findPasswordButton.centerY = self.inputBox.bottom + 17;
    
    self.logInButton.width = self.contentView.width - 80;
    self.logInButton.height = 44;
    self.logInButton.centerX = self.contentView.width / 2;
    self.logInButton.top = self.inputBox.bottom + 64;
    
    self.signUpButton.width = 115;
    self.signUpButton.height = 44;
    self.signUpButton.centerX = self.contentView.width / 2;
    self.signUpButton.top = self.logInButton.bottom + 5;
}

#pragma mark - Actions
- (void)logInAction
{
    [self.view endEditing:YES];
    
    NSString *mobile = self.phoneTextField.text;
    NSString *password = self.passwordTextField.text;

    if(![mobile cy_isPhoneNumber])
    {
        [SVProgressHUD showErrorWithStatus:@"请输入正确的手机号码"];
        [self.phoneTextField becomeFirstResponder];
        return;
    }
    if(password.length == 0)
    {
        [SVProgressHUD showErrorWithStatus:@"请输入密码"];
        [self.passwordTextField becomeFirstResponder];
        return;
    }
    @weakify(self);
    [SVProgressHUD show];
    [CYStandardDataAPI user_loginWithMobile:mobile password:password complete:^(CYResponse *response) {
        @strongify(self);
        if(response.code == 0)
        {
            CYDataAPIRegisterModel *model = [CYDataAPIRegisterModel mj_objectWithKeyValues:response.content];
            CYAuth *auth = [[CYAuth alloc] init];
            auth.token = model.token;
            auth.mobile = model.mobile;
            auth.uid = model.id;
            CYStandardAuthManager.currentAuth = auth;
            [CYStandardAuthManager saveAuth];
            [SVProgressHUD dismiss];
            CYSudokuSignInOpenViewController *sudokuSignInOpenViewController = [[CYSudokuSignInOpenViewController alloc] init];
            [self.navigationController dismissViewControllerAnimated:NO completion:nil];
            [[UIApplication sharedApplication] delegate].window.rootViewController = [[CYNavigationController alloc] initWithRootViewController:sudokuSignInOpenViewController];
        }
        else
        {
            [SVProgressHUD showErrorWithStatus:response.message];
        }
    }];
}

- (void)findPasswordAction
{
    CYFindPasswordViewController *findPasswordViewController = [[CYFindPasswordViewController alloc] init];
    findPasswordViewController.navigationItem.title = @"忘记密码";
    [self.navigationController pushViewController:findPasswordViewController animated:YES];
}

- (void)signUpAction
{
    CYSignUpViewController *signUpViewController = [[CYSignUpViewController alloc] init];
    [self.navigationController pushViewController:signUpViewController animated:YES];
}

#pragma mark - Control Events
- (void)onFindPasswordButtonTouchUpInside
{
    [MobClick event:UM_EVENT_0004];
    [self findPasswordAction];
}

- (void)onLogInButtonTouchUpInside
{
    [MobClick event:UM_EVENT_0005];
    [self logInAction];
}

- (void)onSignUpButtonTouchUpInside
{
    [MobClick event:UM_EVENT_0006];
    [self signUpAction];
}

- (void)onPasswordTextFiedldEditingDidEndOnExit
{
    [MobClick event:UM_EVENT_0005];
    [self logInAction];
}

@end
